using Godot;
using System;
using MonsterDiKe.common;
using MonsterDiKe.utils;

namespace MonsterDiKe.player.script;

public partial class Player : CharacterBody2D
{
    
    /**
     * 动画管理器
     */
    private AnimationPlayer _animationPlayer;
    
    /**
     * 角色移动
     */
    private Vector2 _direction = Vector2.Zero;
    public Vector2 Direction => _direction;

    /**
     * 角色朝向（默认：下）
     */
    private Vector2 _playerDirection = Vector2.Down;
        
    /**
     * 角色精灵
     */
    private Sprite2D _sprite2D;

    /**
     * 角色状态机
     */
    private StateMachine _stateMachine;
    
    

    public override void _Ready()
    {
        GlobalInitializationNode.Player = this;
        // 获取对应节点
        _animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
        _sprite2D = GetNode<Sprite2D>("Sprite2D");
        _stateMachine = GetNode<Node>("StateMachine") as StateMachine;
        CustomAssert.NotNull(_stateMachine,"在玩家脚本中获取不到状态机节点！");
        // 初始化状态机
        _stateMachine.Init();
    }


    public override void _Process(double delta)
    {
        // 给玩家赋予速度
        _direction.X = Input.GetAxis(GlobalKeyConst.WalkLeft, GlobalKeyConst.WalkRight);
        _direction.Y = Input.GetAxis(GlobalKeyConst.WalkUp, GlobalKeyConst.WalkDown);
        // 同时按下两个键会导致角色速递加快，这里对角色的速度做归一化处理
        _direction = _direction.Normalized();
    }

    public override void _UnhandledInput(InputEvent @event)
    {
        // 根据 Velocity 属性移动玩家
        MoveAndSlide();
    }
    
    /**
     * 更新玩家朝向
     */
    public bool UpdatePlayerDirection()
    {
        if (_direction.Equals(Vector2.Zero))
        {
            return false;
        }

        Vector2 curDir = Vector2.Zero;
        if (_direction.X.Equals(Vector2.Zero.X))
        {
            curDir = _direction.Equals(Vector2.Up) ? Vector2.Up : Vector2.Down;
        }
        else if (_direction.Y.Equals(Vector2.Zero.Y))
        {
            curDir = _direction.Equals(Vector2.Right) ? Vector2.Right : Vector2.Left;
        }
        
        if (curDir.Equals(_playerDirection))
        {
            return false;
        }
        _playerDirection = curDir;
        
        // TODO:更新玩家攻击盒方向
        
        float scaleX = 1.0f;
        if (_direction.X > 0)
        {
            scaleX = 1.0f;
        }
        else if (_direction.X < 0)
        {
            scaleX = -1.0f;
        }
        _sprite2D.Scale = new Vector2(scaleX, _sprite2D.Scale.Y);
        return true;
    }
    

    /**
     * 更新玩家动画
     */
    public void UpdateAnimation(string animationPrefixName)
    {
        _animationPlayer.Play(animationPrefixName + GetAnimationDirection());
    }

    /**
     * 根据玩家朝向获取玩对应家动画
     */
    private String GetAnimationDirection()
    {
        if (_playerDirection.Equals(Vector2.Up))
        {
            return PlayerAnimationNameConst.Up;
        }

        if (_playerDirection.Equals(Vector2.Down))
        {
            return PlayerAnimationNameConst.Down;
        }

        return PlayerAnimationNameConst.Idle;
    }
    
}